package com.scs.application.modules.order.service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scs.application.core.model.CommonResult;
import com.scs.application.modules.base.entity.HospitalThirdPlatform;
import com.scs.application.modules.order.dto.OrderDTO;
import com.scs.application.modules.order.dto.PushThirdOrderStatusDTO;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import javax.annotation.Nullable;
import java.util.List;

/**
 * 第三方平台统一接口
 */
public interface ThirdPlatformService {

    default <T> HttpEntity buildHttpEntity(@Nullable T data, String token) {
        HttpHeaders headers = new HttpHeaders();
        if (StringUtils.isNotEmpty(token)) {
            headers.add("X-Token", token);
        }
//        headers.add("Content-Type", "application/json");
//        headers.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        List<MediaType> mediaTypeList = new ArrayList<>();
//        mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
//        headers.setAccept(mediaTypeList);

        return new HttpEntity(data, headers);
    }

    /**
     * 构建接口地址
     *
     * @param apiUrl
     * @return
     */
    default String buildUrl(String apiUrl, String functionUrl) {
        if (apiUrl.trim().endsWith("/")) {
            apiUrl = apiUrl.trim().substring(0, apiUrl.length() - 1);
        }
        if (apiUrl.trim().startsWith("http://")) {
            apiUrl = apiUrl.trim().substring(7);
        }
        if (functionUrl.trim().startsWith("/")) {
            functionUrl = functionUrl.trim().substring(1);
        }
        return String.format("http://%s/%s", apiUrl, functionUrl);
    }

    /**
     * 发送订单到第三方平台
     * @param orderDTOS
     * @return
     */
    void pushOrder(List<OrderDTO> orderDTOS, HospitalThirdPlatform HospitalThirdPlatform);


    void pushOrderStatus(PushThirdOrderStatusDTO dto, HospitalThirdPlatform HospitalThirdPlatform);

    /**
     * 获取订单状态
     *  @param orderDTOS
     * @return
     */
    boolean getOrderStatus(List<OrderDTO> orderDTOS, HospitalThirdPlatform HospitalThirdPlatform);


    /**
     * 获取发货信息
     *  @param orderDTOS
     * @return
     */
    void getDeliveries(List<OrderDTO> orderDTOS, HospitalThirdPlatform HospitalThirdPlatform);

    /**
     * 通知第三方入库
     *  @param orderDTOS
     * @return
     */
    boolean notifyInOrder(List<OrderDTO> orderDTOS, HospitalThirdPlatform HospitalThirdPlatform);

    /**
     * 测试接口
     * @param HospitalThirdPlatform
     * @return
     */
    CommonResult test(HospitalThirdPlatform HospitalThirdPlatform);
}
